package com.example.mybusschedule.ui

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.dimensionResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.hilt.navigation.compose.hiltViewModel
import com.example.mybusschedule.R
import com.example.mybusschedule.data.BusSchedule
import com.example.mybusschedule.ui.theme.MyBusScheduleTheme
import kotlinx.coroutines.launch

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun BusScheduleScreen(
    modifier: Modifier = Modifier,
    viewModel: BusScheduleViewModel = hiltViewModel()
) {
    val uiState = viewModel.state
    val coroutineScope = rememberCoroutineScope()
    Scaffold(
        modifier = modifier,
        topBar = {
            TopAppBar(
                title = if (uiState.isShowingHomepage) {
                    { Text(text = stringResource(id = R.string.app_name)) }
                } else {
                    { Text(text = uiState.currentBusStopSchedules[0].stopName) }
                },
                navigationIcon = {
                    if (!uiState.isShowingHomepage) {
                        IconButton(onClick = viewModel::resetHomeScreenStates) {
                            Icon(
                                imageVector = Icons.AutoMirrored.Filled.ArrowBack,
                                contentDescription = stringResource(id = R.string.back_button)
                            )
                        }
                    }
                }
            )
        },
    ) { contentPadding ->
        Column(
            modifier = Modifier.padding(contentPadding)
        ) {
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(dimensionResource(id = R.dimen.padding_medium)),
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                Text(
                    text = if (uiState.isShowingHomepage) {
                        stringResource(id = R.string.stop_name)
                    } else {
                        stringResource(
                            id = R.string.current_stop_name,
                            uiState.currentBusStopSchedules[0].stopName
                        )
                    },
                    style = MaterialTheme.typography.titleSmall,
                )
                Text(text = stringResource(id = R.string.arrival_time))
            }
            HorizontalDivider()
            BusScheduleList(
                busSchedules = if (uiState.isShowingHomepage) {
                    uiState.busSchedules
                } else {
                    uiState.currentBusStopSchedules
                },
                onRowClick = { stopName ->
                    coroutineScope.launch {
                        viewModel.updateCurrentStop(stopName)
                    }
                },
                isHomepage = uiState.isShowingHomepage
            )
        }
    }
}

@Composable
private fun BusScheduleList(
    busSchedules: List<BusSchedule>,
    onRowClick: (stopName: String) -> Unit,
    modifier: Modifier = Modifier,
    isHomepage: Boolean
) {
    LazyColumn(
        modifier = modifier.padding(dimensionResource(id = R.dimen.padding_medium)),
        verticalArrangement = Arrangement.spacedBy(dimensionResource(id = R.dimen.padding_medium))
    ) {
        items(busSchedules.size) { index ->
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .clickable {
                        if (isHomepage) {
                            onRowClick(busSchedules[index].stopName)
                        }
                    },
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                if (isHomepage) {
                    Text(text = busSchedules[index].stopName)
                    Text(
                        text = busSchedules[index].time(),
                        style = MaterialTheme.typography.titleMedium
                    )
                } else {
                    Text(
                        text = "--",
                        modifier = Modifier.weight(1f),
                        textAlign = TextAlign.Center
                    )
                    Text(
                        text = busSchedules[index].time(),
                        style = MaterialTheme.typography.titleMedium,
                        modifier = Modifier.weight(2f),
                        textAlign = TextAlign.End
                    )
                }

            }
        }
    }
}

@Preview(showBackground = true)
@Composable
fun BusScheduleListPreview() {
    MyBusScheduleTheme {
        Column(
            modifier = Modifier.fillMaxSize()
        ) {
            Row(
                modifier = Modifier.padding(dimensionResource(id = R.dimen.padding_medium))
            ) {
                Text(
                    stringResource(id = R.string.stop_name),
                    textAlign = TextAlign.Center,
                    style = MaterialTheme.typography.titleSmall
                )
                Spacer(modifier = Modifier.weight(1f))
                Text(
                    text = stringResource(id = R.string.arrival_time),
                    style = MaterialTheme.typography.titleSmall
                )
            }
            HorizontalDivider()
            BusScheduleList(
                busSchedules = listOf(
                    BusSchedule(
                        id = 1,
                        stopName = "Stop 1",
                        arrivalTimeInMillis = 1617202800
                    ),
                    BusSchedule(
                        id = 2,
                        stopName = "Stop 1",
                        arrivalTimeInMillis = 1617203520
                    ),
                ),
                onRowClick = {},
                isHomepage = true
            )
        }
    }
}